using System;
using System.ComponentModel;
using System.Configuration;
using System.Web.Services;
using CCS;
using PayPal.Web.Controls;

namespace CCSService_WebRole
{
    /// <summary>
    /// Summary description for IpnService.
    /// </summary>
    public class IpnService : WebService
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public IpnService()
        {
            //CODEGEN: This call is required by the ASP.NET Web Services Designer
            InitializeComponent();
        }

        #region Component Designer generated code

        //Required by the Web Services Designer 
        private IContainer components = null;

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #endregion

        /// <summary>
        /// Main function that will process the input and place the transaction information into the
        /// database.<see cref="IpnTransaction"/>.
        /// </summary>
        /// <param name="ipnTx">The <see cref="IpnTransaction"/> that represents the IPN message.</param>
        /// <returns>Returns TRUE if processing was successful, otherwise returns FALSE.</returns>
        [WebMethod]
        public bool ProcessTransaction(IpnTransaction ipnTx)
        {
            ////Decimal ID = 0;

            try
            {
                #region Insert into Payment_Transaction table and get back ID

                PaymentTransaction paymentTransaction =
                    Procedures.Payment_Insert_Transaction(StorageClientHelper.GetTableStorage(),
                                                          DateTime.Now,
                                                          ipnTx.Business,
                                                          ipnTx.Custom,
                                                          ipnTx.Invoice,
                                                          ipnTx.McCurrency.ToString(),
                                                          ipnTx.McFee,
                                                          ipnTx.McGross,
                                                          ipnTx.Memo,
                                                          ipnTx.PaymentDate,
                                                          ipnTx.PaymentStatus.ToString(),
                                                          ipnTx.PaymentType.ToString(),
                                                          ipnTx.PendingReason.ToString(),
                                                          ipnTx.ReceiverEmail,
                                                          ipnTx.ReceiverID,
                                                          ipnTx.RecurTimes,
                                                          ipnTx.SettleAmount,
                                                          ipnTx.SettleCurrency.ToString(),
                                                          ipnTx.Tax,
                                                          ipnTx.TransactionID,
                                                          ipnTx.TransactionType.ToString());

                #endregion

                #region Insert into Payment_Transaction_Related table

                PaymentTransactionRelated paymentTransactionRelated =
                    Procedures.Payment_Insert_Transaction_Related(StorageClientHelper.GetTableStorage(),
                                                                  paymentTransaction.ID, ipnTx.ExchangeRate,
                                                                  ipnTx.IPNVersion,
                                                                  ipnTx.IsRecurring.ToString(),
                                                                  ipnTx.Option1FieldName,
                                                                  ipnTx.Option1Selection,
                                                                  ipnTx.Option2FieldName,
                                                                  ipnTx.Option2Selection,
                                                                  ipnTx.Password,
                                                                  ipnTx.RawIPN,
                                                                  ipnTx.ReasonCode.ToString(),
                                                                  ipnTx.ReattemptFailedPayments.ToString(),
                                                                  ipnTx.RetryFailedPaymentsDate,
                                                                  ipnTx.Username,
                                                                  ipnTx.VerifySign);

                #endregion

                #region insert into Payment_CustInfo

                Procedures.Payment_Insert_CustInfo(StorageClientHelper.GetTableStorage(),
                                                   paymentTransactionRelated.ID, ipnTx.CustomerInfo.Address1,
                                                   ipnTx.CustomerInfo.Address2,
                                                   ipnTx.CustomerInfo.AddressName,
                                                   ipnTx.CustomerInfo.AddressStatus.ToString(),
                                                   ipnTx.CustomerInfo.BusinessName,
                                                   ipnTx.CustomerInfo.City,
                                                   ipnTx.CustomerInfo.Country,
                                                   ipnTx.CustomerInfo.DayPhoneA,
                                                   ipnTx.CustomerInfo.DayPhoneB,
                                                   ipnTx.CustomerInfo.DayPhoneC,
                                                   ipnTx.CustomerInfo.Email,
                                                   ipnTx.CustomerInfo.FirstName,
                                                   ipnTx.CustomerInfo.LastName,
                                                   ipnTx.CustomerInfo.NightPhoneA,
                                                   ipnTx.CustomerInfo.NightPhoneB,
                                                   ipnTx.CustomerInfo.NightPhoneC,
                                                   ipnTx.CustomerInfo.PayerID,
                                                   ipnTx.CustomerInfo.PayerStatus.ToString(),
                                                   ipnTx.CustomerInfo.State,
                                                   ipnTx.CustomerInfo.Zip);

                #endregion

                #region Insert Payment Shopping Items

                var shoppingItemCollection = new ShoppingItemCollection();
                shoppingItemCollection = ipnTx.Items;
                for (int count = 0; count < shoppingItemCollection.Count; count++)
                {
                    PaymentShoppingItems paymentShoppingItems =
                        Procedures.Payment_Insert_Shopping_Items(StorageClientHelper.GetTableStorage(),
                                                                 paymentTransactionRelated.ID,
                                                                 shoppingItemCollection[count].ItemName,
                                                                 shoppingItemCollection[count].ItemNumber,
                                                                 shoppingItemCollection[count].Option1FieldName,
                                                                 shoppingItemCollection[count].Option1Values,
                                                                 shoppingItemCollection[count].Option2FieldName,
                                                                 shoppingItemCollection[count].Option2Values);
                }

                #endregion
            }
            catch (Exception ex)
            {
                SendMail mail = new SendMail();
                string body;
                string to;
                string subject;

                body = "Customer Name: " + ipnTx.CustomerInfo.FirstName + " " + ipnTx.CustomerInfo.LastName + "<br";
                body += "Customer Email: " + ipnTx.CustomerInfo.Email + "<br>";
                body += "Transaction ID: " + ipnTx.Invoice + "<br>";
                body += "Error Message: " + ex.Message + "<br>";

                to = Properties.Settings.Default.AdminEmailAddress;
                subject = "An error occured while entering the Payment transaction information.";
                mail.Send(subject, body, to);
 
                return false;
            }

            return true;
        }
    }
}